package com.yssw.client.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.yssw.client.constant.WxPayConstant;
import com.yssw.client.domain.vo.EditBalanceVO;
import com.yssw.common.core.domain.entity.SysUser;
import com.yssw.common.utils.DateUtils;
import com.yssw.common.utils.SecurityUtils;
import com.yssw.system.service.ISysUserService;
import com.yssw.util.OrderNumUtil;
import org.omg.CORBA.NO_IMPLEMENT;
import org.omg.PortableInterceptor.NON_EXISTENT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.yssw.client.mapper.RechargeRecordMapper;
import com.yssw.client.domain.RechargeRecord;
import com.yssw.client.service.IRechargeRecordService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 充值记录Service业务层处理
 *
 * @author ice
 * @date 2023-06-16
 */
@Service
public class RechargeRecordServiceImpl implements IRechargeRecordService {
    @Autowired
    private RechargeRecordMapper rechargeRecordMapper;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询充值记录
     *
     * @param id 充值记录主键
     * @return 充值记录
     */
    @Override
    public RechargeRecord selectRechargeRecordById(Long id) {
        return rechargeRecordMapper.selectRechargeRecordById(id);
    }

    /**
     * 查询充值记录列表
     *
     * @param rechargeRecord 充值记录
     * @return 充值记录
     */
    @Override
    public List<RechargeRecord> selectRechargeRecordList(RechargeRecord rechargeRecord) {
        return rechargeRecordMapper.selectRechargeRecordList(rechargeRecord);
    }

    /**
     * 新增充值记录
     *
     * @param rechargeRecord 充值记录
     * @return 结果
     */
    @Override
    public RechargeRecord insertRechargeRecord(RechargeRecord rechargeRecord) {
        rechargeRecord.setCreateTime(DateUtils.getNowDate());
        rechargeRecord.setUpdateBy(SecurityUtils.getUsername());
        rechargeRecord.setCreateBy(SecurityUtils.getUsername());
        rechargeRecord.setStatus(WxPayConstant.NON_PAYMENT);
        rechargeRecord.setPayGate(WxPayConstant.WX_PAYMENT_METHOD);
        String orderCode = OrderNumUtil.randomOrderCode();
        rechargeRecord.setOrderNo(orderCode);
        rechargeRecord.setPayChannelOrderNo(orderCode);
        rechargeRecordMapper.insertRechargeRecord(rechargeRecord);
        return rechargeRecord;
    }

    /**
     * 修改充值记录
     *
     * @param rechargeRecord 充值记录
     * @return 结果
     */
    @Override
    public int updateRechargeRecord(RechargeRecord rechargeRecord) {
        rechargeRecord.setUpdateTime(DateUtils.getNowDate());
        rechargeRecord.setUpdateBy(SecurityUtils.getUsername());
        return rechargeRecordMapper.updateRechargeRecord(rechargeRecord);
    }

    /**
     * 批量删除充值记录
     *
     * @param ids 需要删除的充值记录主键
     * @return 结果
     */
    @Override
    public int deleteRechargeRecordByIds(Long[] ids) {
        return rechargeRecordMapper.deleteRechargeRecordByIds(ids);
    }

    /**
     * 删除充值记录信息
     *
     * @param id 充值记录主键
     * @return 结果
     */
    @Override
    public int deleteRechargeRecordById(Long id) {
        return rechargeRecordMapper.deleteRechargeRecordById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int editStatusAndBalance(EditBalanceVO editBalanceVO) {
        int flag = rechargeRecordMapper.editStatus(editBalanceVO.getOrderNum(), editBalanceVO.getStatus());
        RechargeRecord rechargeRecord = rechargeRecordMapper.selectRechargeRecordByOrderNo(editBalanceVO.getOrderNum());
        SysUser sysUser = userService.selectUserById(rechargeRecord.getMemberId());
        BigDecimal money = rechargeRecord.getMoney();
        BigDecimal balance = sysUser.getBalance();
        BigDecimal multiply = money.multiply(balance);
        sysUser.setBalance(multiply);
        flag += userService.updateUser(sysUser);
        return flag;
    }

}
